package test.testcase.dao;

import org.json.JSONObject;
import test.utils.JsonResult;
import test.utils.Mysql;

public class projectDao {
	
	public static JsonResult getList(String userid) {
		return Mysql.select("select * from project a left join user_project b on a.id=b.projectid where b.userid="+userid);
	}
	
	public static JsonResult getList() {
		return Mysql.select("select * from project a left join user_project b on a.id=b.projectid");
	}
	
	public static JsonResult getParamList(int start, int end, String projectid) {
		return Mysql.select("select * from sysparam a left join project b on a.projectid=b.id where b.id="+projectid+" limit "+start+","+end);
	}
	
	public static JsonResult getParamListByProjectId(String projectid) {
		return Mysql.select("select * from sysparam where projectid="+projectid);
	}

	public static JsonResult add(String projectname, String preurl, String userid, String type) {
		Mysql.excute("insert into project (projectname,preurl,`type`) values('"+projectname+"','"+preurl+"', '"+type+"')");
		JsonResult result = Mysql.select("select * from project where projectname = '"+projectname+"'");
		String id = result.read("data[0].id").toString();
		return Mysql.excute("insert into user_project (userid,projectid) values("+userid+","+id+")");
	}
	
	public static JsonResult del(String id) {
		JsonResult result = Mysql.select("select * from api where projectid="+id);
		if (result.read("data[0]").toJsonObject() != null) {
			return null;
		}
		result = Mysql.select("select * from testcase where projectid="+id);
		if (result.read("data[0]").toJsonObject() != null) {
			return null;
		}
		result = Mysql.select("select * from model where projectid="+id);
		if (result.read("data[0]").toJsonObject() != null) {
			return null;
		}
		result = Mysql.select("select * from sysparam where projectid="+id);
		if (result.read("data[0]").toJsonObject() != null) {
			return null;
		}
		result = Mysql.select("select * from url where projectid="+id);
		if (result.read("data[0]").toJsonObject() != null) {
			return null;
		}
		Mysql.excute("delete from project_typetag where projectid="+id);
		Mysql.excute("delete from timer where jobid in (select id as jobid from job where projectid="+id+")");
		Mysql.excute("delete from `job_excute_log` where jobid in (select id as jobid from job where projectid="+id+")");
		Mysql.excute("delete from job_excute_log where projectid="+id);
		Mysql.excute("delete from timer where projectid="+id);
		Mysql.excute("delete from job_case where jobid in (select id as jobid from job where projectid="+id+")");
		Mysql.excute("delete from job where projectid="+id);
		return Mysql.excute("delete from project where id="+id);
	}
	
	public static JsonResult delsysparam(String projectid, String paramname) {
		return Mysql.excute("delete from sysparam where projectid="+projectid + " and paramname='"+paramname+"'");
	}
	
	public static JsonResult getInfo(String id) {
		return Mysql.select("select * from project where id="+id);
	}
	
	public static JsonResult getSysparamInfo(String paramname, String projectid) {
		return Mysql.select("select * from sysparam where projectid="+projectid+" and paramname='"+paramname+"'");
	}
	
	public static JsonResult save(String projectname, String preurl, String id) {
		return Mysql.excute("update project set projectname='"+projectname+"',preurl='"+preurl+"' where id="+id);
	}
	
	public static JsonResult addsysparam(String paramname, String value, String projectid, String desc) {
		JSONObject object = getSysparamInfo(paramname, projectid).read("data[0]").toJsonObject();
		if (object != null) {
			return null;
		}
		return Mysql.excute("insert into sysparam (projectid,paramname,value,`desc`) values ("+projectid+",'"+paramname+"','"+value+"','"+desc+"')");
	}
	
	public static JsonResult addtypetag(String body, String projectid) {
		JsonResult result = JsonResult.parse(body);
		return Mysql.excute("insert into project_typetag (projectid,typetagname) values ("+projectid+",'"+result.read("typetag").toString()+"')");
	}
	
	public static JsonResult savesysparam(String paramname, String value, String projectid, String desc) {
		return Mysql.excute("update sysparam set value='"+value+"',`desc`='"+desc+"' where paramname='"+paramname+"' and projectid="+projectid);
	}
	
	public static JsonResult typetaglist(String id) {
		return Mysql.select("select * from project_typetag a left join project b on a.projectid=b.id where projectid="+id);
	}
	
	public static JsonResult deltype(String id) {
		return  Mysql.excute("delete from project_typetag where id="+id);
	}
	
	public static JsonResult typeinfo(String id) {
		return Mysql.select("select * from project_typetag a  where a.id="+id);
	}
	
	public static JsonResult savetypetag(String body, String id) {
		JsonResult result = JsonResult.parse(body);
		return Mysql.excute("update project_typetag set typetagname='"+result.read("typetag").toString()+"' where id="+id);
	}
	
	public static JsonResult addusertoproject(String projectid, String userid) {
		return Mysql.excute("replace into user_project values("+userid+","+projectid+")");
	}
	
	public static JsonResult deluserfromproject(String projectid, String userid) {
		return Mysql.excute("delete from user_project where projectid="+projectid+" and userid="+userid);
	}
	
	public static JsonResult tongjiFenlei(String projectid) {
		return Mysql.select("select b.typetagname as name,p.value from (select typetag,count(*) as value from testcase where projectid="+projectid+" group by typetag) p left join `project_typetag` b on p.typetag=b.id");
	}
	
	public static JsonResult tongjiExcuteResult(String projectid) {
		return Mysql.select("SELECT last_excute_result AS name, COUNT(*) AS value FROM testcase WHERE projectid="+projectid+" GROUP BY last_excute_result;");
	}
	
	public static JsonResult getProjectFromJobId(String jobid) {
		return Mysql.select("select * from project where id=(select projectid as id from job where id="+jobid+")");
	}
	
	public static JsonResult getProject(String projectid) {
		return Mysql.select("select * from project where id="+projectid);
	}
	
}
